package com.kch.system.service.impl;

import com.kch.system.domain.Enterprise;
import com.kch.system.domain.SysMember;
import com.kch.system.mapper.EnterpriseMapper;
import com.kch.system.mapper.SysMemberMapper;
import com.kch.system.service.ISysMemberService;
import com.kch.system.vo.MemberListVO;
import com.kch.system.vo.MemberPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

@Service
public class SysMemberServiceImpl implements ISysMemberService {

    @Autowired
    private SysMemberMapper sysMemberMapper;
    @Autowired
    private EnterpriseMapper enterpriseMapper;


    @Override
    public SysMember insertSysMember(Integer userId, String userName) {
        //判断当前用户的企业是否审核通过
        Enterprise enterprise = enterpriseMapper.selectPassByUserId(userId);
        if(ObjectUtils.isEmpty(enterprise)){
            throw new RuntimeException("当前用户的企业没有审核通过,无法申请会员");
        }
        if(sysMemberMapper.selectIsMember(userId) > 0){
            throw new RuntimeException("已经申请或已经是会员，请勿重复申请");
        }
        SysMember member = new SysMember();
        member.setUserId(userId);
        member.setEnterTime(new Date());
        member.setCreateBy(userName);
        member.setReviewStatus("0");
        sysMemberMapper.insert(member);
        return member;
    }

    @Override
    public int selectIsMember(Integer userId) {
        return sysMemberMapper.selectIsMember(userId);
    }

    @Override
    public List<MemberPageVO> selectMemberPage(MemberPageVO pageVO) {
        return sysMemberMapper.selectMemberPage(pageVO);
    }

    @Override
    public MemberPageVO selectMemberVO(Integer id) {
        return sysMemberMapper.selectMemberVO(id);
    }

    @Override
    public SysMember selectMemberById(Integer id) {
        return sysMemberMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateById(SysMember sysMember) {
        return sysMemberMapper.updateByPrimaryKey(sysMember);
    }

    @Override
    public int updateStatus(Integer id) {
        SysMember sysMember = sysMemberMapper.selectByPrimaryKey(id);
        String status = ("0".equals(sysMember.getStatus())? "1" : "0");
        sysMember.setStatus(status);
        return sysMemberMapper.updateByPrimaryKey(sysMember);
    }

    @Override
    public List<MemberListVO> selectMemberList() {
        return sysMemberMapper.selectMemberList();
    }
}
